//
//  LBVisitorView.swift
//  LBNewWeiBo
//
//  Created by bin on 15/10/26.
//  Copyright © 2015年 bin. All rights reserved.
/*
添加界面元素,懒加载控件

转轮
房子
文字标签
注册按钮
登录按钮
需要注意消息文字需要设置尺寸sizeToFit
*/

import UIKit

// 登录\注册点击事件的协议方法
protocol LBVisitorViewDelegate : NSObjectProtocol {
    func visitorViewRegister()
    func visitorViewLogin()
}

class LBVisitorView: UIView {

    // 属性代理者
    weak var visitorViewDelegate : LBVisitorViewDelegate?
    //MARK: -按钮的点击事件
    // 登录按钮的点击事件
     func loginClick(){
//        print(__FUNCTION__)
        visitorViewDelegate?.visitorViewLogin()
    }
    // 注册按钮的点击事件
     func registerClick(){
//        print(__FUNCTION__)
        visitorViewDelegate?.visitorViewRegister()
        
    }
    
    //MARK: - 实现当前视图的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)

        self.prepareUI() // 调用添加UI子控件
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 准备UI
    /// 准备UI
    private func prepareUI() {
        addSubview(iconView)
        addSubview(shadeView)
        addSubview(homeView)
        addSubview(titleView)
        addSubview(registerButton)
        addSubview(loginButton)
    
        // 设置视图的颜色
        self.backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        // 布局子控件
        self.setAutoLatout()
    }
    
    /// 不同页面设置不同信息
    func setupInfo(imageName: String, title: String){
        
        self.iconView.image = UIImage(named:imageName)
        self.titleView.text = title
        self.titleView.sizeToFit() // 原始尺寸
        
        homeView.hidden = true // 隐藏小房子
        
        sendSubviewToBack(shadeView) // 将遮盖物添加到最下面
    }
    
    /// 自动布局设置控件位置
    private func setAutoLatout() {
        
        // 设置自动约束一定要设置成虚的false
        self.iconView.translatesAutoresizingMaskIntoConstraints = false
        self.homeView.translatesAutoresizingMaskIntoConstraints = false
        self.titleView.translatesAutoresizingMaskIntoConstraints = false
        self.registerButton.translatesAutoresizingMaskIntoConstraints = false
        self.loginButton.translatesAutoresizingMaskIntoConstraints = false
        self.shadeView.translatesAutoresizingMaskIntoConstraints = false
        // 布局轮子视图
        // 公式： view1.attr1 = view2.attr2 * multiplier + constant
        // CenterX
        addConstraint(NSLayoutConstraint(item: self.iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // centerY
        addConstraint(NSLayoutConstraint(item: self.iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
        
        // 布局房子视图
        // centerX
        addConstraint(NSLayoutConstraint(item: self.homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // centerY
        addConstraint(NSLayoutConstraint(item: self.homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        // 布局label文字
    //        // centerX
        addConstraint(NSLayoutConstraint(item: self.titleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    //        // 顶部
        addConstraint(NSLayoutConstraint(item: self.titleView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
    //        // 宽度
        addConstraint(NSLayoutConstraint(item: self.titleView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        
        // 布局注册按钮（左边）
        addConstraint(NSLayoutConstraint(item: self.registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        // 头部
        addConstraint(NSLayoutConstraint(item: self.registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 宽度
        addConstraint(NSLayoutConstraint(item: self.registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高度
        addConstraint(NSLayoutConstraint(item: self.registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
        
        
        // 布局登录按钮（右边）
        addConstraint(NSLayoutConstraint(item: self.loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        // 头部
        addConstraint(NSLayoutConstraint(item: self.loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 宽度
        addConstraint(NSLayoutConstraint(item: self.loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高度
        addConstraint(NSLayoutConstraint(item: self.loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
        
        // 布局遮盖视图 头部
        addConstraint(NSLayoutConstraint(item: self.shadeView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
       // 左边
       addConstraint(NSLayoutConstraint(item: self.shadeView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        // 右边
       addConstraint(NSLayoutConstraint(item: self.shadeView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        // 下边
        addConstraint(NSLayoutConstraint(item: self.shadeView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
    }
    
    //MARK: -首页轮子的旋转动画
    func startRotationAnimation(){
   
        // 创建基本动画，选择旋转方式
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI // 角度
        animation.duration = 20 // 持续时间
        animation.repeatCount = MAXFLOAT // 最大重复次数
        animation.removedOnCompletion = false;
        iconView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView .layer.convertTime(CACurrentMediaTime() , fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
    }
    
    
    //MARK: -懒加载
    /// 遮罩图片
    private lazy var shadeView: UIImageView = {
       let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
    }()
    
    /// 轮子
    private lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()

    /// 房子
    private lazy var homeView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    
    /// 文字标签
    private lazy var titleView: UILabel = {
        let title = UILabel() // 创建文本视图
        title.text = "关注一些人，看看有什么惊喜."
        title.textColor = UIColor.grayColor()
        title.numberOfLines = 0 // 自动换行
        title.textAlignment = NSTextAlignment.Center
        title.sizeToFit()
        
        return title
        
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let registerBtn = UIButton() // 创建按钮
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal) //设置背景图片
        registerBtn.setTitle("注册", forState: UIControlState.Normal) // 按钮的标识文字
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal) // 字体颜色
        
        // 登录按钮的点击事件
        registerBtn.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerBtn
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let loginButton = UIButton() // 创建按钮
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal) //设置背景图片
        loginButton.setTitle("登录", forState: UIControlState.Normal) // 按钮的标识文字
        loginButton.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal) // 字体颜色
        // 登录按钮的点击事件
        loginButton.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginButton
        }()
}
